package com.zjw.zy.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zjw.zy.App;
import com.zjw.zy.AppService;
import com.zjw.zy.MainActivity;
import com.zjw.zy.bean.User;
import com.zjw.zy.constant.AppConstant;

import org.jetbrains.annotations.NotNull;

import map.MapActivity;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/9/26 17:07
 */
public final class AppUtils {

    private static final String IS_FIRST_INSTALL = "IS_FIRST_INSTALL";

    private static App mApp;

    private static MainActivity mMainActivity;
    private static MapActivity mMapActivity;
    private static AppService mService;


    @SuppressLint("StaticFieldLeak")
    private static Activity currentActivity;

    //两个用户
    private static User self;
    private static User love;


    private static boolean isFront = false;
    private static boolean isMainActivityFront = false;

    public static void init(App app) {
        mApp = app;

        mApp.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            private int resumeCount = 0;

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                resumeCount++;
                isFront = resumeCount > 0;

                if (activity instanceof MainActivity) {
                    isMainActivityFront = true;
                }

                currentActivity = activity;
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                resumeCount--;
                isFront = resumeCount > 0;

                if (activity instanceof MainActivity) {
                    isMainActivityFront = false;
                }

                currentActivity = null;
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }

    public static Activity getCurrentActivity() {
        return currentActivity;
    }

    public static App getApp() {
        return mApp;
    }

    public static boolean isFront() {
        return isFront;
    }

    public static boolean isMainActivityFront() {
        return isMainActivityFront;
    }

    /**
     * 关键条件判断，只能使用一次
     */
    public static boolean install(@NotNull Runnable task) {
        boolean isFirstInstall = SharePrefUtils.getBoolean(IS_FIRST_INSTALL, true);
        if (isFirstInstall) {
            SharePrefUtils.putBoolean(IS_FIRST_INSTALL, false);
            task.run();
        }
        return isFirstInstall;
    }

    public static void addMain(@NotNull MainActivity mainActivity) {
        mMainActivity = mainActivity;
    }

    public static void addMap(@NotNull MapActivity mapActivity) {
        mMapActivity = mapActivity;
    }

    public static MainActivity getMainActivity() {
        return mMainActivity;
    }

    public static AppService getService() {
        return mService;
    }

    public static void removeMain() {
        mMainActivity = null;
    }

    public static void removeMap() {
        mMapActivity = null;
    }

    public static void initService(AppService appService) {
        mService = appService;
    }

    public static boolean isAccess() {
        return SharePrefUtils.getBoolean(AppConstant.IS_APP_ACCESS, false);
    }

    public static MapActivity getMapActivity() {
        return mMapActivity;
    }

    @NotNull
    public static String getSelfAccess() {
        User self = getSelf();
        return self == null ? "" : self.getUsername();
    }

    @Nullable
    public static User getSelf() {
        if (self == null) {
            String me = SharePrefUtils.getString(AppConstant.APP_ME_ACCESS_KEY, "");
            self = GsonUtils.fromJson(me, User.class);
        }
        return self;
    }

    @Nullable
    public static User getLove() {
        if (love == null) {
            String me = SharePrefUtils.getString(AppConstant.APP_LOVER_ACCESS_KEY, "");
            love = GsonUtils.fromJson(me, User.class);
        }
        return love;
    }


    @NotNull
    public static String getLoveAccess() {
        User love = getLove();
        return love == null ? "" : love.getUsername();
    }
}
